package activemq;

import dao.PeopleDao;
import dao.impl.PeopleDaoImpl;
import utils.ActiveMq;
import utils.ParsingUtil;

import javax.jms.*;
import java.io.IOException;

public class ActiveMqCustomer {

    public static void main(String[] args) throws JMSException, IOException {
        Connection connection = ActiveMq.getConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(ActiveMq.TOPIC_NAME);
        session.createProducer(topic);
        MessageConsumer customer = session.createConsumer(topic);
        StringBuilder stringBuilder = new StringBuilder();
        customer.setMessageListener(message -> {
            if (message != null && message instanceof TextMessage) {
                try {
                    if (((TextMessage) message).getText().endsWith(".txt")) {
                        PeopleDao peopleDao = new PeopleDaoImpl();
                        peopleDao.save(ParsingUtil.parsingTxt(((TextMessage) message).getText()));
                    } else if (((TextMessage) message).getText().endsWith(".xlsx")) {
                        PeopleDao peopleDao = new PeopleDaoImpl();
                        peopleDao.save(ParsingUtil.parsingExcel(((TextMessage) message).getText()));
                    } else {
                        PeopleDao peopleDao = new PeopleDaoImpl();
                        peopleDao.save(ParsingUtil.parsingJSON(((TextMessage) message).getText()));
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        System.in.read();
        customer.close();
        session.close();
        connection.close();

    }
}
